#! /bin/bash
# Call this script to check the format every c[pp] & h[pp] file
# in the current and parent directories against the defined clang-format style.
#
# Returns 1 on an incorrect format

FILES="$(find . -type f -name \*.hpp -or -name \*.h -or -name \*.cpp -or -name \*.c)"

DIFFS=""
for cfile in $FILES; do
    DIFF=$(diff "$cfile" <(clang-format -style=file $cfile))
    if [[ -n "$DIFF" ]]; then
        DIFFS="$DIFFS $cfile"
    fi
done

if [[ -n "$DIFFS" ]]; then
    echo "The following files are not formatted correctly:"
    for cfile in $DIFFS; do
        echo "$cfile"
    done
    exit 1
else
    echo "All files are formatted correctly"
    exit 0
fi
